package com.curvebreakers.wheresmystuff.tests.activities;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.activities.EntrySortFragment;
import com.curvebreakers.wheresmystuff.activities.HomeActivity;

import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;

public class DateStringValidation extends ActivityInstrumentationTestCase2<HomeActivity> {

	private String[] options;
	private HomeActivity home;
	private Instrumentation homeInst;
	
	public DateStringValidation() {
		super(HomeActivity.class);
	}

	@Before
	public void setUp() throws Exception {
		super.setUp();
		homeInst= getInstrumentation();
		home = getActivity();
		options = home.getResources().getStringArray(com.curvebreakers.wheresmystuff.R.array.DateEntries);

	}
	
	/* The reason I'm hard-coding the comparison dates is because constructing a test that works continuously would require the Calendar class of Java, which
	 * would essentially duplicate the method and render the comparison meaningless. With hard-coded dates changed according to the Calendar class's rules,
	 * it is easier to see if the poorly-named (sorry) setDates method is actually doing its job.
	 */
	@Test
	public void testWeek() throws Throwable{
		home.runOnUiThread(new Runnable() {
			public void run() {
				home.getActionBar().setSelectedNavigationItem(1);
				EntrySortFragment esfragment = (EntrySortFragment) home.getActiveFragment();
				home.getFragmentManager().beginTransaction().replace(R.id.fragment_container, esfragment);
				home.getFragmentManager().beginTransaction().commit();
				home.getFragmentManager().executePendingTransactions();
				if (esfragment.getActivity() == null) System.out.println("Help!");
				else{
					String output = esfragment.setDates(options[1]);
					System.out.println(output);
					String answer = "minDate=2013-04-05+00:00:00&"; // One week ago, should be true.
					System.out.println(answer);
					String wrong = "minDate=2013-03-29+00:00:00&"; // Two weeks ago, should be false.
					Assert.assertTrue(output.equals(answer));
					Assert.assertFalse(output.equals(wrong));
				}
			}
		});
		homeInst.waitForIdleSync();
	}
	
	public void testMonth() throws Throwable{
		home.runOnUiThread(new Runnable() {
			public void run() {
				home.getActionBar().setSelectedNavigationItem(1);
				EntrySortFragment esfragment = (EntrySortFragment) home.getActiveFragment();
				home.getFragmentManager().beginTransaction().replace(R.id.fragment_container, esfragment);
				home.getFragmentManager().beginTransaction().commit();
				home.getFragmentManager().executePendingTransactions();
				if (esfragment.getActivity() == null) System.out.println("Help!");
				else{
					String output = esfragment.setDates(options[2]);
					System.out.println(output);
					String answer = "minDate=2013-03-12+00:00:00&"; // One month ago, should be true.
					System.out.println(answer);
					String wrong = "minDate=2013-02-12+00:00:00&"; // Two months ago, should be false.
					Assert.assertTrue(output.equals(answer));
					Assert.assertFalse(output.equals(wrong));
				}
			}
		});
		homeInst.waitForIdleSync();
	}

	public void testYear() throws Throwable{
		home.runOnUiThread(new Runnable() {
			public void run() {
				home.getActionBar().setSelectedNavigationItem(1);
				EntrySortFragment esfragment = (EntrySortFragment) home.getActiveFragment();
				home.getFragmentManager().beginTransaction().replace(R.id.fragment_container, esfragment);
				home.getFragmentManager().beginTransaction().commit();
				home.getFragmentManager().executePendingTransactions();
				if (esfragment.getActivity() == null) System.out.println("Help!");
				else{
					String output = esfragment.setDates(options[3]);
					System.out.println(output);
					String answer = "minDate=2012-04-12+00:00:00&"; // One year ago to the day, should be true.
					System.out.println(answer);
					String wrong = "minDate=2011-04-12+00:00:00&"; // Two years ago, should be wrong.
					Assert.assertTrue(output.equals(answer));
					Assert.assertFalse(output.equals(wrong));
				}
			}
		});
		homeInst.waitForIdleSync();
	}
	
	
	@After
	public void tearDown() throws Exception{
		getActivity().finish();
		super.tearDown();
	}

}
